package com.gitee.srurl.snow.backwards;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 时钟回拨工厂
 *
 * @author zxw
 * @date 2023/4/22 11:30
 */
public class ClockBackwardsFactory {

    public static Class<? extends AbstractBackwards> getHandle(String type) {

        Class<? extends AbstractBackwards> backwards = null;
        ClockBackwardsHandleEnums clockBackwardsHandleEnums = ClockBackwardsHandleEnums.find(type);
        if (clockBackwardsHandleEnums != null) {
            switch (clockBackwardsHandleEnums) {
                case REJECT_CLOCK_BACKWARDS -> backwards = RejectClockBackwards.class;
                case BORROW_FUTURE_TIME -> backwards = BorrowFutureTime.class;
                case USE_NEW_WORK -> backwards = UseNewWork.class;
            }
        }

        return backwards == null ? RejectClockBackwards.class : backwards;
    }

    @Getter
    @AllArgsConstructor
    public enum ClockBackwardsHandleEnums {

        /**
         * 拒绝时钟回拨
         */
        REJECT_CLOCK_BACKWARDS("0"),

        /**
         * 借以后的时间
         */
        BORROW_FUTURE_TIME("1"),

        /**
         * 创建新的工作ID
         */
        USE_NEW_WORK("2"),

        ;

        private final String type;

        public static ClockBackwardsHandleEnums find(String type) {
            if (StrUtil.isBlank(type)) {
                return null;
            }
            for (ClockBackwardsHandleEnums value : values()) {
                if (StrUtil.equals(value.getType(), type)) {
                    return value;
                }
            }

            return null;
        }
    }
}
